using System.Collections;
using UnityEngine;

public class NPC : MonoBehaviour
{
	public bool ismoving;

	public PlayerManager PlayerManager;

	public float velocityX;

	public float velocityY;

	public Collider2D[] CollidersToDisableOnWalk;

	public bool AnimateAutomatically;

	public bool FinishedMoveTo;

	private Vector2 pos;

	public Animator NPCAnimator;

	public Coroutine CurrentWalkRoutine;

	[Header("Range : -1 to 1")]
	public float DefaultStandDirectionX;

	public float DefaultStandDirectionY;

	private void Start()
	{
		NPCAnimator.SetFloat("MOVEMENTX", DefaultStandDirectionX);
		NPCAnimator.SetFloat("MOVEMENTY", DefaultStandDirectionY);
	}

	private void Update()
	{
		Animate();
	}

	public void WalkToPosition(Vector3 TargetPosition, float Speed)
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		if (CurrentWalkRoutine != null)
		{
			((MonoBehaviour)this).StopCoroutine(CurrentWalkRoutine);
		}
		CurrentWalkRoutine = ((MonoBehaviour)this).StartCoroutine(WalkTo(TargetPosition, Speed));
	}

	public void RotateNPC(Vector2 Dir)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		NPCAnimator.SetFloat("MOVEMENTX", Dir.x);
		NPCAnimator.SetFloat("MOVEMENTY", Dir.y);
	}

	private IEnumerator WalkTo(Vector3 TargetPosition, float Speed)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		ismoving = true;
		while (true)
		{
			Vector3 val = TargetPosition - ((Component)this).transform.position;
			if (!(((Vector3)(ref val)).sqrMagnitude > Mathf.Epsilon))
			{
				break;
			}
			((Component)this).transform.position = Vector3.MoveTowards(((Component)this).transform.position, TargetPosition, Speed * Time.deltaTime);
			yield return null;
			CalculateVelocity();
		}
		FinishedMoveTo = true;
		ismoving = false;
		CurrentWalkRoutine = null;
	}

	private void CalculateVelocity()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		velocityX = (((Component)this).transform.position.x - pos.x) / Time.deltaTime;
		velocityY = (((Component)this).transform.position.y - pos.y) / Time.deltaTime;
		pos = Vector2.op_Implicit(((Component)this).transform.position);
	}

	private void Animate()
	{
		if (AnimateAutomatically)
		{
			NPCAnimator.SetBool("MOVING", ismoving);
		}
		if (ismoving && AnimateAutomatically)
		{
			NPCAnimator.SetFloat("MOVEMENTX", velocityX);
			NPCAnimator.SetFloat("MOVEMENTY", velocityY);
		}
		Collider2D[] collidersToDisableOnWalk = CollidersToDisableOnWalk;
		for (int i = 0; i < collidersToDisableOnWalk.Length; i++)
		{
			((Behaviour)collidersToDisableOnWalk[i]).enabled = !ismoving;
		}
	}

	public void PlayAnimation(string AnimationName)
	{
		NPCAnimator.Play(AnimationName);
	}
}
